use axum::{
    async_trait,
    extract::{FromRequest, RequestParts},
    http::StatusCode,
    Extension,
};
use cores::internal_error;

use crate::RBAC;

pub struct RBACMiddleware;

#[async_trait]
impl<B> FromRequest<B> for RBACMiddleware
    where
        B: Send,
{
    type Rejection = (StatusCode, String);

    async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
        let Extension(rbac) = Extension::<RBAC>::from_request(req)
            .await
            .map_err(internal_error)?;

        let enforcer = rbac.enforcer.clone();

        tracing::info!("RBACMiddleware url:{:#?}", req.uri());

        Ok(Self)
    }
}
